<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class AppLanguage extends CI_Language {
    var $language    = array();
    var $is_loaded    = array();

    function AppLanguage()
    {
        parent::CI_Language();
        log_message('debug', "AppLanguage Class Initialized");
    }
    
    function module_load($langfile = '', $idiom = '', $return = FALSE)
    {
        $arr = Modules::search_path($langfile);
        $arr[0] = $arr[3].'/'.$arr[0];
        $module_name = $arr[0];
        $langfile = $arr[1];
        $langfile = str_replace(EXT, '', str_replace('_lang.', '', $langfile)).'_lang'.EXT;

        if (in_array($langfile, $this->is_loaded, TRUE))
        {
            return;
        }
        
        if ($idiom == '')
        {
            $CI =& get_instance();
            $deft_lang = $CI->config->item('language');
            $idiom = ($deft_lang == '') ? 'english' : $deft_lang;
        }

        if (file_exists(MODPATH.$module_name.'/language/'.$idiom.'/'.$langfile))
        {
            include(MODPATH.$module_name.'/language/'.$idiom.'/'.$langfile);
        }
        else
        {
            show_error('Unable to load the requested language file: language/'.$langfile);
        }

        if ( ! isset($lang))
        {
            log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
            return;
        }
        
        if ($return == TRUE)
        {
            return $lang;
        }
        
        $this->is_loaded[] = $langfile;
        $this->language = array_merge($this->language, $lang);
        unset($lang);
        
        log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
        return TRUE;
    }
}